<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-tabs>
        <!-- 角色管理 -->
        <el-tab-pane label="角色管理">
          <!-- 新增角色按钮 -->
          <el-row style="height: 60px">
            <el-button
              icon="el-icon-plus"
              size="small"
              type="primary"
              @click="$refs.addRole.isShow = true"
            >新增角色</el-button>
          </el-row>
          <!-- 表格 -->
          <el-table border stripe :data="list">
            <el-table-column label="序号" width="120" type="index" />
            <el-table-column label="角色名称" width="240" prop="name" />
            <el-table-column label="描述" prop="description" />
            <el-table-column label="操作">
              <!-- 作用域插槽 -->
              <template slot-scope="{row}">
                <el-button
                  size="small"
                  type="success"
                  @click="openDialog(row.id)"
                >分配权限</el-button>
                <el-button
                  size="small"
                  type="primary"
                  @click="editHandler(row.id)"
                >编辑</el-button>
                <el-button
                  size="small"
                  type="danger"
                  @click="delHandler(row.id)"
                >删除</el-button>
              </template>
            </el-table-column>
          </el-table>
          <!-- 分页组件 -->
          <el-row
            type="flex"
            justify="center"
            align="middle"
            style="height: 60px"
          >
            <!-- 分页组件 -->
            <el-pagination
              :current-page="queryData.page"
              :page-sizes="[2, 3, 4, 5]"
              :page-size="queryData.pagesize"
              layout="total, sizes, prev, pager, next, jumper"
              :total="total"
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
            />
          </el-row>
        </el-tab-pane>
        <!-- 公司信息 -->
        <el-tab-pane label="公司信息">
          <el-alert
            title="对公司名称、公司地址、营业执照、公司地区的更新，将使得公司资料被重新审核，请谨慎修改"
            type="info"
            show-icon
            :closable="false"
          />
          <el-form label-width="120px" style="margin-top: 50px">
            <el-form-item label="公司名称">
              <el-input
                v-model="companyData.name"
                disabled
                style="width: 400px"
              />
            </el-form-item>
            <el-form-item label="公司地址">
              <el-input
                v-model="companyData.companyAddress"
                disabled
                style="width: 400px"
              />
            </el-form-item>
            <el-form-item label="邮箱">
              <el-input
                v-model="companyData.mailbox"
                disabled
                style="width: 400px"
              />
            </el-form-item>
            <el-form-item label="备注">
              <el-input
                v-model="companyData.remarks"
                type="textarea"
                :rows="3"
                disabled
                style="width: 400px"
              />
            </el-form-item>
          </el-form>
        </el-tab-pane>
      </el-tabs>
      <!-- 添加修改弹框 -->
      <addRole ref="addRole" @update="initData" />
      <!-- 角色权限 -->
      <set-auth ref="setAuth" />
    </div>
  </div>
</template>

<script>

import {
  getRoleInfo,
  getRoleListData,
  getCompanyInfo,
  delRoleInfo
} from '@/api/settings'
import addRole from './components/add-role.vue'
import SetAuth from './components/set-auth.vue'
export default {
  name: 'Settings',
  components: {
    addRole,
    SetAuth
  },
  data() {
    return {
      currentPage4: 1,
      queryData: {
        page: 1,
        pagesize: 2
      },
      list: [],
      total: 0, // 总数
      companyData: {}
    }
  },
  created() {
    this.initData()
    this.getCompanyInfo()
  },
  methods: {
    async openDialog(id) {
      await this.$refs['setAuth'].getRoleAuth(id)
      await this.$refs['setAuth'].getPermissionList()
    },
    // 获取角色列表
    async initData() {
      const { rows, total } = await getRoleListData(this.queryData)
      console.log(rows)
      this.list = rows
      this.total = total
    },
    // 获取公司列表
    async getCompanyInfo() {
      const res = await getCompanyInfo(this.$store.getters.companyId)
      console.log(res)
      this.companyData = res
    },
    // 删除角色列表
    async delHandler(val) {
      await this.$confirm('确定删除吗？')
      await delRoleInfo(val.id)
      this.$message.success('删除成功')
      // 不是第一页，且该页面只有一个元素，且删除成功；页码-1，获取数据
      if (this.queryData.page !== 1 && this.list.length === 1) {
        this.queryData.page--
      }
      this.initData()
    },
    handleSizeChange(val) {
      // val是每页最新数量
      this.queryData.pagesize = val
      this.initData()
      console.log(`每页 ${val} 条`)
    },
    handleCurrentChange(val) {
      // val就是最新页码
      this.queryData.page = val
      this.initData()
      console.log(`当前页: ${val}`)
    },
    // 修改点击事件，获取改行数据
    async editHandler(row) {
      console.log(this.$refs)
      const res = await getRoleInfo(row.id)
      this.$refs.addRole.formData = res
      this.$refs.addRole.isShow = true
    }
  }
}
</script>

<style lang="scss" scoped></style>
